package com.ruoyi.gouhuoruku.service.impl;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.gouhuoruku.domain.GouhuoRuku2;
import com.ruoyi.gouhuoruku.domain.GouhuoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gouhuoruku.mapper.GouhuoRukuMapper;
import com.ruoyi.gouhuoruku.domain.GouhuoRuku;
import com.ruoyi.gouhuoruku.service.IGouhuoRukuService;
import com.ruoyi.common.core.text.Convert;

/**
 * 购货入库Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-05
 */
@Service
public class GouhuoRukuServiceImpl implements IGouhuoRukuService 
{
    @Autowired
    private GouhuoRukuMapper gouhuoRukuMapper;

    /**
     * 查询购货入库
     * 
     * @param id 购货入库主键
     * @return 购货入库
     */
    @Override
    public GouhuoRuku selectGouhuoRukuById(Long id)
    {
        return gouhuoRukuMapper.selectGouhuoRukuById(id);
    }

    @Override
    public String selectGouhuoDanhaoByLastOne() {
        return gouhuoRukuMapper.selectGouhuoDanhaoByLastOne();
    }

    @Override
    public Long selectGouhuoRukuByJiner(String gouhuoCangku,String today) {
        String[] date_ = today.split("-");
        String firstday = null,lastday = null;
        switch (Integer.parseInt(date_[1])){
            case 10:
                firstday=date_[0]+"-10-1";lastday=date_[0]+"-10-31";
                break;
            case 8:
                firstday=date_[0]+"-8-1";lastday=date_[0]+"-8-31";
                break;
            case 7:
                firstday=date_[0]+"-7-1";lastday=date_[0]+"-7-31";
                break;
            case 5:
                firstday=date_[0]+"-5-1";lastday=date_[0]+"-5-31";
                break;
            case 3:
                firstday=date_[0]+"-3-1";lastday=date_[0]+"-3-31";
                break;
            case 1:
                firstday=date_[0]+"-1-1";lastday=date_[0]+"-1-31";
                break;
            case 12:
                firstday=date_[0]+"-12-1";lastday=date_[0]+"-12-31";
                break;

            case 4:
                firstday=date_[0]+"-4-1";lastday=date_[0]+"-4-30";
                break;
            case 6:
                firstday=date_[0]+"-6-1";lastday=date_[0]+"-6-30";
                break;
            case 9:
                firstday=date_[0]+"-9-1";lastday=date_[0]+"-9-30";
                break;
            case 11:
                firstday=date_[0]+"-11-1";lastday=date_[0]+"-11-30";
                break;

            case 2:
                firstday=date_[0]+"-2-1";lastday=date_[0]+"-2-29";
                break;
            default:break;
        }
        return gouhuoRukuMapper.selectGouhuoRukuByjiner(gouhuoCangku,firstday,lastday);
    }

    /**
     * 查询购货入库列表
     * 
     * @param gouhuoRuku 购货入库
     * @return 购货入库
     */
    @Override
    public List<GouhuoRuku> selectGouhuoRukuList(GouhuoRuku gouhuoRuku)
    {
        return gouhuoRukuMapper.selectGouhuoRukuList(gouhuoRuku);
    }

    /**
     * 查询购货入库列表
     *
     * @return 购货入库集合
     */
    public List<GouhuoRuku> selectAllGouhuoRukuList(String cangkuName){
        return gouhuoRukuMapper.selectAllGouhuoRukuList(cangkuName);
    }

    /**
     * 新增购货入库
     * 
     * @param gouhuoRuku 购货入库
     * @return 结果
     */
    @Override
    public int insertGouhuoRuku(GouhuoRuku gouhuoRuku)
    {
        return gouhuoRukuMapper.insertGouhuoRuku(gouhuoRuku);
    }

    /**
     * 修改购货入库
     * 
     * @param gouhuoRuku 购货入库
     * @return 结果
     */
    @Override
    public int updateGouhuoRuku(GouhuoRuku gouhuoRuku)
    {
        return gouhuoRukuMapper.updateGouhuoRuku(gouhuoRuku);
    }

    /**
     * 批量删除购货入库
     * 
     * @param ids 需要删除的购货入库主键
     * @return 结果
     */
    @Override
    public int deleteGouhuoRukuByIds(String ids)
    {
        return gouhuoRukuMapper.deleteGouhuoRukuByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除购货入库信息
     * 
     * @param id 购货入库主键
     * @return 结果
     */
    @Override
    public int deleteGouhuoRukuById(Long id)
    {
        return gouhuoRukuMapper.deleteGouhuoRukuById(id);
    }

    @Override
    public List<String> selectGouhuoRuKuByDate(String cangku, String startDay, String endDay) {

        Date startDate = null;
        Date endDate = null;
        try {
            startDate = new Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDay).getTime());
            endDate = new Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDay).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<String> xlist = new ArrayList<>();
        String jiner,cangkuName;
        int index = 1;

        if (cangku != null) {
            List<GouhuoVo> list = gouhuoRukuMapper.selectGouhuoRuKuByDate(startDate,endDate);
            for (GouhuoVo gouhuoVo : list) {
                cangkuName = gouhuoVo.getCangKu();
               if (cangku.equals(cangkuName)&& index<=10){
                   index += 1;
                   jiner = gouhuoVo.getTotaljiner().toString();
                   LocalDateTime date = gouhuoVo.getDate();
                   String month =  date.getMonthValue() +"月";
                   String day = date.getDayOfMonth()+"日";
                   String date1 = month + day;
                   String result = date1+"@"+cangkuName+"@"+jiner;
                   xlist.add(result);
               }
            }
        } else {
            List<GouhuoVo> list = gouhuoRukuMapper.selectGouhuoRuKuByDate1(startDate,endDate);
            for (GouhuoVo gouhuoVo : list) {
                cangkuName = gouhuoVo.getCangKu();
                jiner = gouhuoVo.getTotaljiner().toString();
                cangkuName = gouhuoVo.getCangKu();
                LocalDateTime date = gouhuoVo.getDate();
                String month =  date.getMonthValue() +"月";
                String day = date.getDayOfMonth()+"日";
                String date1 = month + day;
                String result = date1+"@"+cangkuName+"@"+jiner;
                xlist.add(result);
            }
        }
        System.out.println(xlist);
        return xlist;
    }
}
